package com.bootdo.system.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import com.bootdo.system.domain.ScanRecord;

@Mapper
public interface ScanRecordMapper {

	@Select("select * from tb_scanRecord where id = #{id}")
	ScanRecord get(Long id);
	
	@Select("<script>" +
	"select * from tb_scanRecord " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"customerId != null and customerId != ''\">"+ "and customerId = #{customerId} " + "</if>" + 
		  	"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<ScanRecord> list(Map<String,Object> map);
	
	@Select("<script>" +
	"select count(*) from tb_scanRecord " + 
			"<where>" + 
			  "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
			  "<if test=\"customerId != null and customerId != ''\">"+ "and customerId = #{customerId} " + "</if>"
			+ "</where>"+ 
			"</script>")
	int count(Map<String,Object> map);
	
	@Select("select count(*) from tb_scanRecord where customerId = #{customerId} ")
	int getTotalByCustomerId(Long customerId);
	
	
	@Select("<script>" +
			"select customerId,count(*) total from tb_scanRecord " + 
					"<where>" + 
			  		  "<if test=\"customerId != null and customerId != ''\">"+ "and customerId = #{customerId} " + "</if>" + 
				  	"</where>"+ 
			  		  " group by customerId " + 
					"</script>")
   List<ScanRecord> countList(Map<String,Object> map);
	
	
	@Insert("insert into tb_scanRecord (`customerId`, `createTime`)"
	+ "values (#{customerId}, now())")
	@Options(useGeneratedKeys=true)
	int save(ScanRecord scanRecord);
	
}
